#ifndef THINK_THINK_HOOK_H
#define THINK_THINK_HOOK_H

static char* think_hook =
	"\n"
	"// +----------------------------------------------------------------------\n"
	"// | ThinkPHP [ WE CAN DO IT JUST THINK ]\n"
	"// +----------------------------------------------------------------------\n"
	"// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.\n"
	"// +----------------------------------------------------------------------\n"
	"// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )\n"
	"// +----------------------------------------------------------------------\n"
	"// | Author: liu21st <liu21st@gmail.com>\n"
	"// +----------------------------------------------------------------------\n"
	"\n"
	"namespace think;\n"
	"\n"
	"class Hook\n"
	"{\n"
	"    /**\n"
	"     * @var array 标签\n"
	"     */\n"
	"    private static $tags = [];\n"
	"\n"
	"    /**\n"
	"     * 动态添加行为扩展到某个标签\n"
	"     * @access public\n"
	"     * @param  string $tag      标签名称\n"
	"     * @param  mixed  $behavior 行为名称\n"
	"     * @param  bool   $first    是否放到开头执行\n"
	"     * @return void\n"
	"     */\n"
	"    public static function add($tag, $behavior, $first = false)\n"
	"    {\n"
	"        isset(self::$tags[$tag]) || self::$tags[$tag] = [];\n"
	"\n"
	"        if (is_array($behavior) && !is_callable($behavior)) {\n"
	"            if (!array_key_exists(\'_overlay\', $behavior) || !$behavior[\'_overlay\']) {\n"
	"                unset($behavior[\'_overlay\']);\n"
	"                self::$tags[$tag] = array_merge(self::$tags[$tag], $behavior);\n"
	"            } else {\n"
	"                unset($behavior[\'_overlay\']);\n"
	"                self::$tags[$tag] = $behavior;\n"
	"            }\n"
	"        } elseif ($first) {\n"
	"            array_unshift(self::$tags[$tag], $behavior);\n"
	"        } else {\n"
	"            self::$tags[$tag][] = $behavior;\n"
	"        }\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 批量导入插件\n"
	"     * @access public\n"
	"     * @param  array   $tags      插件信息\n"
	"     * @param  boolean $recursive 是否递归合并\n"
	"     * @return void\n"
	"     */\n"
	"    public static function import(array $tags, $recursive = true)\n"
	"    {\n"
	"        if ($recursive) {\n"
	"            foreach ($tags as $tag => $behavior) {\n"
	"                self::add($tag, $behavior);\n"
	"            }\n"
	"        } else {\n"
	"            self::$tags = $tags + self::$tags;\n"
	"        }\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 获取插件信息\n"
	"     * @access public\n"
	"     * @param  string $tag 插件位置(留空获取全部)\n"
	"     * @return array\n"
	"     */\n"
	"    public static function get($tag = \'\')\n"
	"    {\n"
	"        if (empty($tag)) {\n"
	"            return self::$tags;\n"
	"        }\n"
	"\n"
	"        return array_key_exists($tag, self::$tags) ? self::$tags[$tag] : [];\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 监听标签的行为\n"
	"     * @access public\n"
	"     * @param  string $tag    标签名称\n"
	"     * @param  mixed  $params 传入参数\n"
	"     * @param  mixed  $extra  额外参数\n"
	"     * @param  bool   $once   只获取一个有效返回值\n"
	"     * @return mixed\n"
	"     */\n"
	"    public static function listen($tag, &$params = null, $extra = null, $once = false)\n"
	"    {\n"
	"        $results = [];\n"
	"\n"
	"        foreach (static::get($tag) as $key => $name) {\n"
	"            $results[$key] = self::exec($name, $tag, $params, $extra);\n"
	"\n"
	"            // 如果返回 false，或者仅获取一个有效返回则中断行为执行\n"
	"            if (false === $results[$key] || (!is_null($results[$key]) && $once)) {\n"
	"                break;\n"
	"            }\n"
	"        }\n"
	"\n"
	"        return $once ? end($results) : $results;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 执行某个行为\n"
	"     * @access public\n"
	"     * @param  mixed  $class  要执行的行为\n"
	"     * @param  string $tag    方法名（标签名）\n"
	"     * @param  mixed  $params 传人的参数\n"
	"     * @param  mixed  $extra  额外参数\n"
	"     * @return mixed\n"
	"     */\n"
	"    public static function exec($class, $tag = \'\', &$params = null, $extra = null)\n"
	"    {\n"
	"        App::$debug && Debug::remark(\'behavior_start\', \'time\');\n"
	"\n"
	"        $method = Loader::parseName($tag, 1, false);\n"
	"\n"
	"        if ($class instanceof \\Closure) {\n"
	"            $result = call_user_func_array($class, [ & $params, $extra]);\n"
	"            $class  = \'Closure\';\n"
	"        } elseif (is_array($class)) {\n"
	"            list($class, $method) = $class;\n"
	"\n"
	"            $result = (new $class())->$method($params, $extra);\n"
	"            $class  = $class . \'->\' . $method;\n"
	"        } elseif (is_object($class)) {\n"
	"            $result = $class->$method($params, $extra);\n"
	"            $class  = get_class($class);\n"
	"        } elseif (strpos($class, \'::\')) {\n"
	"            $result = call_user_func_array($class, [ & $params, $extra]);\n"
	"        } else {\n"
	"            $obj    = new $class();\n"
	"            $method = ($tag && is_callable([$obj, $method])) ? $method : \'run\';\n"
	"            $result = $obj->$method($params, $extra);\n"
	"        }\n"
	"\n"
	"        if (App::$debug) {\n"
	"            Debug::remark(\'behavior_end\', \'time\');\n"
	"            Log::record(\'[ BEHAVIOR ] Run \' . $class . \' @\' . $tag . \' [ RunTime:\' . Debug::getRangeTime(\'behavior_start\', \'behavior_end\') . \'s ]\', \'info\');\n"
	"        }\n"
	"\n"
	"        return $result;\n"
	"    }\n"
	"\n"
	"}\n"
	"\n"
;
#endif